﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using AmiEmuLib.Configuration;
using AmiEmuLib.Configuration.KnownRomConfig;
using System.Xml;
using TomanuExtensions;
using TomanuExtensions.Utils;

namespace AmiEmuLib.AmigaConfiguration.MemoryConfig
{
    /// <summary>
    /// Information about rom file.
    /// </summary>
    [DebuggerDisplay("FilePath = {FilePath}, AmigaRomType = {RomType}")]
    public class RomInfo : XmlBase
    {
        /// <summary>
        /// If file not exists and we have rom with same SHA1 use it. Try this first. 
        /// </summary>
        public string SHA1 = "";

        /// <summary>
        /// Path to rom, if file exists and SHA1 match use it. If file exists and hash not match
        /// and we have file with this hash use it. Try this after SHA1.
        /// </summary>
        public string FilePath = "";

        /// <summary>
        /// Rom class. Try this at end. If few rom exists with this type use rom priority to resolve.
        /// </summary>
        public RomType RomType;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_reader"></param>
        public RomInfo(XmlReader a_reader)
            : base(a_reader)
        {
        }

        /// <summary>
        /// If we should load this rom.
        /// </summary>
        public bool Required
        {
            get
            {
                return (FilePath != "") || (SHA1 != "") || (RomType != RomType.Undefined);
            }
        }

        /// <summary>
        /// Try to find rom file. First try by SHA1. If exists load it. Its almost impossible to
        /// have two roms with the same SHA1. Next try to find rom by filepath. At least try 
        /// by rom type.
        /// </summary>
        /// <returns></returns>
        public string FindRom()
        {
            var roms1 = KnownRoms.Roms.Concat(UnknownRoms.Roms);

            var rom = roms1.FirstOrDefault(r => r.SHA1 == SHA1);
            if (rom != null)
            {
                if (rom is KnownRom)
                {
                    if ((rom as KnownRom).Exists)
                        return rom.FilePath;
                }
                else
                    return rom.FilePath;
            }

            if (FileUtils.IsFilePathValid(FilePath))
            {
                if (new FileInfo(FilePath).Exists)
                {
                    byte[] data = FileUtils.ReadFile(FilePath);
                    if (data != null)
                        return FilePath;
                }
            }

            if (RomType != RomType.Undefined)
            {
                var rom1 = KnownRoms.GetAvailableDefaultKnownRom(RomType);
                if (rom1 != null)
                    return rom1.FilePath;
            }

            return "";
        }

        protected override void ReadXml(XmlReader a_reader)
        {
            a_reader.ReadStartElement(GetType().Name);

            FilePath = a_reader.ReadElementContentAsString("FilePath");
            SHA1 = a_reader.ReadElementContentAsString("SHA1");
            RomType = a_reader.ReadElementContentAsEnum<RomType>("RomType");

            a_reader.ReadEndElement();
        }

        public override void WriteXml(XmlWriter a_writer)
        {
            a_writer.WriteStartElement(GetType().Name);

            a_writer.WriteElement("FilePath", FilePath);
            a_writer.WriteElement("SHA1", SHA1);
            a_writer.WriteElement("RomType", RomType);

            a_writer.WriteEndElement();
        }
    }
}
